package com.pomelo.tiku.activities

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.blankj.utilcode.util.LogUtils
import com.pomelo.tiku.R
import com.pomelo.tiku.beans.RegisterBean
import com.pomelo.tiku.constants.SharePreferenceConstants
import com.pomelo.tiku.constants.UrlConstants.Companion.BASE_URL
import com.pomelo.tiku.constants.UrlConstants.Companion.URL_USER_REGISTER
import com.pomelo.tiku.impls.RegisterModel
import com.pomelo.tiku.impls.RegisterPresenter
import com.pomelo.tiku.interfaces.Contract
import com.pomelo.tiku.managers.SharedPreferenceManager
import com.pomelo.tiku.utils.ClickUtils
import com.pomelo.tiku.views.CommonToast
import kotlinx.android.synthetic.main.activity_oritation_vocation.*
import kotlinx.android.synthetic.main.activity_register.*
import java.lang.ref.WeakReference
import java.util.HashMap

class RegisterActivity : BaseActivity(), View.OnClickListener,Contract.IRegisterView {
    private var registerModel:Contract.IRegisterModel? = null
    private var registerPresenter:Contract.IRegisterPresenter? = null
    private var data:HashMap<String, String>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
        initView()
    }
    private fun initView() {
       btn_register.setOnClickListener(this@RegisterActivity)
    }
    private fun register(username: String,password:String) {
        val registerUrl = BASE_URL + URL_USER_REGISTER
        val header = HashMap<String, String>()
        data = HashMap<String, String>()
        data!!["username"] = username
        data!!["password"] = password
        registerModel = RegisterModel(this@RegisterActivity)
        val reference = WeakReference(this)
        registerPresenter = RegisterPresenter(registerModel,reference.get())
        if(registerPresenter != null) {
            (registerPresenter as RegisterPresenter).request(registerUrl,header,data)
        }
    }

    override fun onClick(view: View?) {
        when(view!!.id) {
            R.id.btn_register -> {
                if(ClickUtils.isDoubleClick(btn_register)) {
                    return
                }
                val username = edit_username.text.toString().trim()
                val password = edit_pwd.text.toString().trim()
                val confirmPassword = edit_pwd_confirm.text.toString().trim()
                if(TextUtils.isEmpty(username)) {
                    CommonToast.show(this@RegisterActivity,"请输入用户名")
                    return
                }
                if(TextUtils.isEmpty(password)) {
                    CommonToast.show(this@RegisterActivity,"请输入密码")
                    return
                }
                if(TextUtils.isEmpty(confirmPassword)) {
                    CommonToast.show(this@RegisterActivity,"请输入确认密码")
                    return
                }

                if(password != confirmPassword) {
                    CommonToast.show(this@RegisterActivity,"密码和确认密码不一致")
                    return
                }
                register(username,password)
            }
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        registerModel = null
        registerPresenter = null
        data = null
    }

    override fun registerSuccess(data: RegisterBean?) {
        CommonToast.show(this@RegisterActivity,"注册成功")
        SharedPreferenceManager.getInstance(this@RegisterActivity).save(SharePreferenceConstants.KEY_TOKEN,data!!.token)
        startActivity(Intent(this@RegisterActivity,LoginActivity::class.java))
        finish()
    }

    override fun registerFailed(message: String?) {
        CommonToast.show(this@RegisterActivity,message)
    }
}